package com.ztz.musicappdemo.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ztz.musicappdemo.MyDbHelper;
import com.ztz.musicappdemo.R;

public class RegisterActivity extends BaseActivity {
    EditText edUserName,edPwd1,edPwd2;
    Button btn_Register;
    MyDbHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper=new MyDbHelper(this,"UserInfoStore.db",null,1);
        edUserName=(EditText)findViewById(R.id.ed_UserName);
        edPwd1=(EditText)findViewById(R.id.ed_RegisterPwd1);
        edPwd2=(EditText)findViewById(R.id.ed_RegisterPwd2);
        btn_Register=(Button)findViewById(R.id.btnRegister);
        btn_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName=edUserName.getText().toString();
                String registerPwd1=edPwd1.getText().toString();
                String registerPwd2=edPwd2.getText().toString();
                if(userName.isEmpty()||registerPwd1.isEmpty()||registerPwd2.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入账号信息", Toast.LENGTH_SHORT).show();
                }else {
                    if(!registerPwd1.equals(registerPwd2)){
                        Toast.makeText(RegisterActivity.this, "两次密码输入不对,请重新输入", Toast.LENGTH_SHORT).show();
                        edPwd1.setText("");
                        edPwd2.setText("");
                    }else {
                        SQLiteDatabase db=dbHelper.getWritableDatabase();
                        ContentValues values=new ContentValues();
                        values.put("userName",userName);
                        values.put("password",registerPwd1);
                        long id=db.insert("UserInfo",null,values);
                        if(id!=-1){
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }else {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
    }
}
